﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuKou.Web.BLL.Service;
using LuKou.Shopspace.Domains;
using LuKou.Shopspace.Services;

namespace LuKou.Web.BLL.Extensions
{
    /// <summary>
    /// 商品类扩展
    /// </summary>
    public static class GoodsExt
    {
        private static IGoodsSvr goods_svr;
        private static IShopSvr shop_svr;


        static GoodsExt()
        {
            goods_svr = ServiceHelper.Get<IGoodsSvr>("IGoodsSvr");
            shop_svr = ServiceHelper.Get<IShopSvr>("IShopSvr");
        }

        /// <summary>
        /// 获取商品的主图片(最多5张)
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public static List<GoodsImage> GetMajorImages(this Goods goods)
        {
            GoodsImageQueryParam param = new GoodsImageQueryParam();
            param.AddParamOfGoodsID(goods.ID);
            param.AddParamOfIsMajor(true);
            return goods_svr.GetGoodsImageList(param, true, 0, 5);
        }

        /// <summary>
        /// 获取商品的图片
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public static List<GoodsImage> GetImages(this Goods goods)
        {
            GoodsImageQueryParam param = new GoodsImageQueryParam();
            param.AddParamOfGoodsID(goods.ID);
            param.AddParamOfIsMajor(false);
            return goods_svr.GetGoodsImageList(param, true, 0, 5);
        }

        /// <summary>
        /// 获取商品所在的店铺数据
        /// </summary>
        /// <param name="goods"></param>
        public static Shop GetShop(this Goods goods)
        {
            return shop_svr.GetShopDetail(goods.ShopID, true);
        }

        /// <summary>
        /// 获取商品的属性数据
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public static List<CategoryPropValue> GetAttrs(this Goods goods)
        {
            List<CategoryPropValue> list = new List<CategoryPropValue>();
            foreach (var attr in goods.Attribs)
            {
                var item = CategoryService.GetGoodsCategory(attr.CategoryID, attr.Pid, attr.Vid);
                if (item != null) list.Add(item);
            }

            return list;
        }

        /// <summary>
        /// 获取商品的组合价格信息
        /// </summary>
        /// <param name="goods"></param>
        public static List<CategoryPropValue> GetSKUDate(this Goods goods)
        {
            List<CategoryPropValue> list = new List<CategoryPropValue>();
            foreach (var item in goods.SKUs)
            {
                foreach (var skuProp in item.SKUProps)
                {
                    if (list.Find(c =>c.CategoryID == item.CategoryID && c.Pid == skuProp.Pid && c.Vid == skuProp.Vid)==null)
                    {
                        list.Add(CategoryService.GetGoodsCategory(item.CategoryID, skuProp.Pid, skuProp.Vid));
                    }
                }
            }

            return list;
        }
    }
}
